package com.tac.xiangmu3.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tac.xiangmu3.common.CostomException;
import com.tac.xiangmu3.common.R;
import com.tac.xiangmu3.pojo.User;
import com.tac.xiangmu3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserComntroller {
    @Autowired
    private UserService userService;
    /**
     * 添加会员
     * @param user
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody User user){
        if(user.getUsername()==null||user.getPassword()==null){
            throw new CostomException("请输入完整信息");
        }
        //先判断名字有没有占用
        String username = user.getUsername();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User one = userService.getOne(queryWrapper);
        if(one==null){
            //没有
            user.setUpdateDate(LocalDateTime.now());
            user.setAddDate(LocalDateTime.now());
            userService.save(user);
            return R.success(null,"添加成功");
        }else {
            //有
            return  R.error("用户已存在");
        }
    }

    /**
     * 批量删除用户(逻辑删除)
     * @param uids
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody ArrayList<Long> uids){
        if(uids==null){
            throw new CostomException("请选择要删除的用户");
        }
        for(Long uid:uids){
            User user = userService.getById(uid);
            user.setStatus(0);
            userService.updateById(user);
        }
        return R.success(null,"删除成功");
    }

    /**
     *
     * @param user
     * @return
     */
    @PutMapping("/updata")
    public R update(@RequestBody User user){
        if(user.getUsername()==null||user.getPassword()==null){
            throw new CostomException("请输入完整信息");
        }
        userService.updateById(user);
        return R.success(null,"修改成功");
    }

    /**
     * 模糊查询分页查询 日期查询
     * @param current
     * @param size
     * @param username
     * @return
     */
    @GetMapping("/page")
    public R page(long current, long size,String username,String beginTime,String endTime){
        Page<User> pageinfo = new Page<>(current, size);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getStatus,1);
       // queryWrapper.lt(z!=null,User::getUpdateDate,z).gt(q!=null,User::getUpdateDate,q);
        queryWrapper.between(beginTime!=null&&endTime!=null,User::getUpdateDate,beginTime,endTime);
        queryWrapper.like(username!=null,User::getUsername,username);
        userService.page(pageinfo,queryWrapper);
        return R.success(pageinfo,"查询成功");
    }
    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public R selectById(Long id){
        User user = userService.getById(id);
        return R.success(user,"查询成功");
    }

}
